﻿<%@ WebHandler Language="C#" Class="uploadImg1" %>


using System;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public class uploadImg1 : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
       // context.Response.ContentType = "text/plain";
           context.Response.Write("Hello World");
        string s1 = context.Request["param"];
        string s3 = context.Request["param1"];
        
        
        int i = s1.IndexOf(",");
        string s2 = s1.Substring(i+1);
   
        context.Response.Write("Base 65 Code = "+s2);
        System.Drawing.Image img = Base64ToImage(s2);
       context.Response.Write(img);
        System.Drawing.Image imag = img;
        
        
        SqlConnection conn = null;

        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
        conn.Open();
        SqlCommand cmd = new SqlCommand("insert into agent_add(email,profile_pic) values(@email,@profile_pic)", conn);
        cmd.Parameters.AddWithValue ("@email",s1);
    
        cmd.Parameters.Add("@profile_pic", SqlDbType.Image, 0).Value = ConvertImageToByteArray(imag, System.Drawing.Imaging.ImageFormat.Jpeg);
       

        cmd.ExecuteNonQuery();
        context.Response.Write("Record Submitted");
    }
    public bool IsReusable {
        get {
            return false;
        }
    }
    public System.Drawing.Image Base64ToImage(string base64String)
    {
        // Convert Base64 String to byte[]
        byte[] imageBytes = Convert.FromBase64String(base64String);
        System.IO.MemoryStream ms = new System.IO.MemoryStream(imageBytes, 0, imageBytes.Length);

        // Convert byte[] to Image
        ms.Write(imageBytes, 0, imageBytes.Length);
        System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
        return image;

    }
    private byte[] ConvertImageToByteArray(System.Drawing.Image imageToConvert, System.Drawing.Imaging.ImageFormat formatOfImage)
    {
        byte[] Ret;
        try
        {
            using (MemoryStream ms = new MemoryStream())
            {
                imageToConvert.Save(ms, formatOfImage);
                Ret = ms.ToArray();
            }
        }
        catch (Exception) { throw; }
        return Ret;
    }

}